#region Usings

using System;
using Ninject.Core.Logging;
using Karma.Framework.Core.Domain;
using Karma.Framework.Core.IoC;
using Karma.Framework.Core.Metadata;
using Karma.Framework.Core.Repository;

#endregion

namespace Karma.Framework.Core.Services
{
    public abstract class ApplicationServiceBase<T, TR> : IApplicationService<T>
        where T : DomainObject
        where TR : IRepository<T>
    {
        protected static ILogger LOG = IoCFactory.Resolve<ILoggerFactory>().GetLogger(
            typeof (ApplicationServiceBase<T, TR>));

        protected ApplicationServiceBase(TR repo)
        {
            Repository = repo;
        }

        #region Implementation of IQueryable<T>

        public virtual QueryResult Find(QuerySpecification specification)
        {
            return Repository.Find(QueryFinder.BuscaConsulta(specification));
        }

        #endregion

        #region Implementation of IEntityOperations<T>

        public virtual T Get(Int64 primaryKey)
        {
            return Repository.Get(primaryKey);
        }

        public virtual void Delete(Int64 primaryKey)
        {
            Repository.Delete(primaryKey);
        }

        public virtual T Update(T instance)
        {
            return Repository.Update(instance);
        }

        public virtual T Save(T instance)
        {
            return Repository.Save(instance);
        }

        #endregion

        public TR Repository { get; set; }
    }
}